/*
 * $Id: ApplicationComponentListenerUnitTest.java 37 2009-08-15 23:19:07Z
 * aaron.day $
 */
package org.crazydays.gameplan.swing;


import java.awt.Component;
import java.awt.event.ComponentEvent;
import java.io.File;

import org.junit.*;

import static org.junit.Assert.*;

import static org.easymock.EasyMock.*;
import static org.easymock.classextension.EasyMock.*;

import org.crazydays.gameplan.Application;
import org.crazydays.gameplan.Preferences;
import org.crazydays.junit.TestHelper;

/**
 * ApplicationComponentListenerUnitTest
 */
public class ApplicationComponentListenerUnitTest
{
    /** test helper */
    protected TestHelper testHelper =
        new TestHelper(ApplicationComponentListenerUnitTest.class);

    /**
     * Cleanup test helper.
     */
    @After
    public void tearDown()
    {
        testHelper.tearDown();
    }

    /**
     * Setup test case user properties.
     */
    @Before
    public void setUp()
    {
        // copy user properties
        File userProperties =
            new File(testHelper.getSandbox(),
                '.' + Preferences.PROPERTIES_FILENAME);

        testHelper.copy(userProperties, "gameplan.properties");

        System.setProperty("user.home", testHelper.getSandbox()
            .getAbsolutePath());
    }

    /**
     * Test componentMoved(ComponentEvent).
     */
    @Test
    public void testComponentMoved()
    {
        // objects
        Preferences preferences = new Preferences();
        int x = 42;
        int y = 24;

        // mocks
        ComponentEvent event = createMock(ComponentEvent.class);
        Component component = createMock(Component.class);

        // expect
        expect(event.getSource()).andReturn(component);
        expect(component.getX()).andReturn(x);
        expect(event.getSource()).andReturn(component);
        expect(component.getY()).andReturn(y);

        // replay
        replay(event, component);

        // setup
        ApplicationComponentListener listener =
            new ApplicationComponentListener(preferences);

        // test
        listener.componentMoved(event);

        // verify
        verify(event, component);

        assertEquals("x", x, preferences.getInt(Application.APPLICATION_X));
        assertEquals("y", y, preferences.getInt(Application.APPLICATION_Y));
    }

    /**
     * Test componentResized(ComponentEvent).
     */
    @Test
    public void testComponentResized()
    {
        // objects
        Preferences preferences = new Preferences();
        int width = 24;
        int height = 42;

        // mocks
        ComponentEvent event = createMock(ComponentEvent.class);
        Component component = createMock(Component.class);

        // expect
        expect(event.getSource()).andReturn(component);
        expect(component.getWidth()).andReturn(width);
        expect(event.getSource()).andReturn(component);
        expect(component.getHeight()).andReturn(height);

        // replay
        replay(event, component);

        // setup
        ApplicationComponentListener listener =
            new ApplicationComponentListener(preferences);

        // test
        listener.componentResized(event);

        // verify
        verify(event, component);

        assertEquals("width", width, preferences
            .getInt(Application.APPLICATION_WIDTH));
        assertEquals("height", height, preferences
            .getInt(Application.APPLICATION_HEIGHT));
    }
}
